package com.JingYan.util;

import com.JingYan.exception.CustomException;
import com.JingYan.validation.group.Create;
import org.apache.poi.ss.formula.functions.T;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author DongJiaQi
 * @create 2023/2/10 15:02
 * @Description: validation 校验工具类
 */
public class ValidationUtil {

    static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    /**
     * 手动调校验方法
     * @param bean 要校验的类
     * @param createClass  校验分组
     */
    public static void validatorBean(Object bean, Class<T> createClass) {
        StringBuilder sb = new StringBuilder();
        Set<ConstraintViolation<Object>> validate = validator.validate(bean, createClass);

        for (ConstraintViolation<Object> dwIndicatordictionaryDtoConstraintViolation : validate) {
            String message = dwIndicatordictionaryDtoConstraintViolation.getMessage();
            sb.append(message + " ;");
        }
        if (sb.length() > 0) {
            throw new CustomException(sb.toString());
        }
    }
}
